package app.qrcode.browser.chat.model.dao

import android.annotation.SuppressLint
import android.content.ContentValues
import android.content.Context
import android.util.Log
import app.qrcode.browser.chat.model.bean.GroupInfo
import app.qrcode.browser.chat.model.bean.InvitationInfo
import app.qrcode.browser.chat.model.bean.UserInfo
import app.qrcode.browser.chat.model.db.DBHelper

/**
 * Created by DomenCai on 2022/3/12.
 */
class InviteTableDao(context: Context) {
    val mHelper = DBHelper(context, "DBHelper.db", 1)
    fun addInvitation(invitationInfo: InvitationInfo) {
        val db = mHelper.readableDatabase
        val values = ContentValues().apply {
            put(InviteTable.COL_REASON, invitationInfo.reason)
            put(InviteTable.COL_STATUS, invitationInfo.status?.ordinal)
        }
        val user = invitationInfo.user
        if (user != null) {
            values.put(InviteTable.COL_USER_HXID, user.hxId)
            values.put(InviteTable.COL_USER_NAME, user.name)
        } else {
            values.put(InviteTable.COL_GROUP_HXID, invitationInfo.group?.groupId)
            values.put(InviteTable.COL_GROUP_NAME, invitationInfo.group?.groupName)
            values.put(InviteTable.COL_USER_HXID, invitationInfo.group?.invitePerson)
        }
        db.replace(InviteTable.TAB_NAME, null, values)
    }

    @SuppressLint("Range")
    fun getInvitations(): List<InvitationInfo> {
        val db = mHelper.readableDatabase
        val sql = "select * from " +
                InviteTable.TAB_NAME
        val cursor = db.rawQuery(sql, null)
        val invitationInfos = ArrayList<InvitationInfo>()
        var invitationInfo: InvitationInfo
        while (cursor.moveToNext()) {
//            val invitationInfo =
//                int2InviteStatus(cursor.getInt(cursor.getColumnIndex(InviteTable.COL_STATUS)))?.let {
//                    InvitationInfo(
//                        null, null, cursor.getString(cursor.getColumnIndex(InviteTable.COL_REASON)),
//                        it
//                    )
//                }
            val status = int2InviteStatus(cursor.getInt(cursor.getColumnIndex(InviteTable.COL_STATUS)))
            val reason = cursor.getString(cursor.getColumnIndex(InviteTable.COL_REASON))
            
            val groupId = cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_HXID))
            if (groupId == null) {
                val userInfo = UserInfo(
                    cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_NAME)),
                    cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_HXID)),
                    cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_NAME)),
                    null
                )
//                val status = int2InviteStatus(cursor.getInt(cursor.getColumnIndex(InviteTable.COL_STATUS)))
//                val reason = cursor.getString(cursor.getColumnIndex(InviteTable.COL_REASON))
                invitationInfo=InvitationInfo(userInfo,null,reason,status)
            } else {
                val groupInfo =
                    GroupInfo(
                        cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_NAME)),
                        cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_HXID)),
                        cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_HXID))
                    )
                invitationInfo=InvitationInfo(null,groupInfo,reason,status)
            }

            invitationInfos.add(invitationInfo)
        }
        cursor.close()
        Log.w("cdm", "InviteTableDao.getInvitations -> $invitationInfos")
        return invitationInfos
    }

    fun int2InviteStatus(intStatus: Int): InvitationInfo.InvitationStatus? {
        if (intStatus == InvitationInfo.InvitationStatus.NEW_INVITE.ordinal) {
            return InvitationInfo.InvitationStatus.NEW_INVITE
        }
        if (intStatus == InvitationInfo.InvitationStatus.INVITE_ACCEPT.ordinal) {
            return InvitationInfo.InvitationStatus.INVITE_ACCEPT
        }
        if (intStatus == InvitationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER.ordinal) {
            return InvitationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER
        }
        if (intStatus == InvitationInfo.InvitationStatus.NEW_GROUP_INVITE.ordinal) {
            return InvitationInfo.InvitationStatus.NEW_GROUP_INVITE
        }
        if (intStatus == InvitationInfo.InvitationStatus.NEW_GROUP_APPLICATION.ordinal) {
            return InvitationInfo.InvitationStatus.NEW_GROUP_APPLICATION
        }
        if (intStatus == InvitationInfo.InvitationStatus.GROUP_INVITE_ACCEPTED.ordinal) {
            return InvitationInfo.InvitationStatus.GROUP_INVITE_ACCEPTED
        }
        if (intStatus == InvitationInfo.InvitationStatus.GROUP_APPLICATION_ACCEPTED.ordinal) {
            return InvitationInfo.InvitationStatus.GROUP_APPLICATION_ACCEPTED
        }
        if (intStatus == InvitationInfo.InvitationStatus.GROUP_INVITE_DECLINED.ordinal) {
            return InvitationInfo.InvitationStatus.GROUP_INVITE_DECLINED
        }
        if (intStatus == InvitationInfo.InvitationStatus.GROUP_APPLICATION_DECLINED.ordinal) {
            return InvitationInfo.InvitationStatus.GROUP_APPLICATION_DECLINED
        }
        if (intStatus == InvitationInfo.InvitationStatus.GROUP_ACCEPT_INVITE.ordinal) {
            return InvitationInfo.InvitationStatus.GROUP_ACCEPT_INVITE
        }
        if (intStatus == InvitationInfo.InvitationStatus.GROUP_ACCEPT_APPLICATION.ordinal) {
            return InvitationInfo.InvitationStatus.GROUP_ACCEPT_APPLICATION
        }
        if (intStatus == InvitationInfo.InvitationStatus.GROUP_REJECT_APPLICATION.ordinal) {
            return InvitationInfo.InvitationStatus.GROUP_REJECT_APPLICATION
        }
        if (intStatus == InvitationInfo.InvitationStatus.GROUP_REJECT_INVITE.ordinal) {
            return InvitationInfo.InvitationStatus.GROUP_REJECT_INVITE
        }
        return null
    }

    fun removeInvitation(hxId: String?) {
        if (hxId == null) {
            return
        }
        val db = mHelper.readableDatabase
        db.delete(
            InviteTable.TAB_NAME, "${InviteTable.COL_USER_HXID} =?",
            listOf(hxId).toTypedArray()
        )
    }

    fun updateInvitationStatus(invitationStatus: InvitationInfo.InvitationStatus, hxId: String?) {
        if (hxId == null) {
            return
        }

        val db = mHelper.readableDatabase
        val values = ContentValues().apply {
            put(InviteTable.COL_STATUS, invitationStatus.ordinal)
        }
        db.update(
            InviteTable.TAB_NAME, values, "${InviteTable.COL_USER_HXID} =?",
            listOf(hxId).toTypedArray()
        )

    }
}